from rest_framework import serializers

from app.models import FMUser
from app.models.address import MailingAddress


class MailingAddressListSerializer(serializers.ListSerializer):
    def update(self, instance, validated_data):
        pass

    def create(self, validated_data):
        address_list = [MailingAddress(**item) for item in validated_data]
        return MailingAddress.objects.bulk_create(address_list)

    @classmethod
    def many_init(cls, *args, **kwargs):
        kwargs['child'] = cls()
        return MailingAddressListSerializer(*args, **kwargs)


class MailingAddressSerializer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=FMUser.objects.all(), required=False)

    class Meta:
        model = MailingAddress
        fields = '__all__'  # ('id', 'province', 'city', 'area', 'address', 'addressee', 'phone')
        # list_serializer_class = MailingAddressListSerializer

    def to_representation(self, instance: MailingAddress):
        return {
            'id': instance.id,
            'province': instance.province,
            'province_name': instance.get_province_display(),
            'city': instance.city,
            'city_name': instance.get_city_display(),
            'area': instance.area,
            'area_name': instance.get_area_display(),
            'address': instance.address,
            'addressee': instance.addressee,
            'phone': instance.phone,
            'is_default': instance.is_default
        }
